package admin

import (
	"exercise/application/yiran/app/libs"
	"exercise/application/yiran/app/request/adminRequest"
	"exercise/application/yiran/app/service/admin"
	"github.com/gin-gonic/gin"
)

type scoreRuleController struct {
}

// List 列表
func (*scoreRuleController) List(ctx *gin.Context) {
	request := new(adminRequest.ScoreRuleListRequest)
	err := libs.BindRequest(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	ret, err := admin.NewScoreRuleService(ctx).List(request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.Success(ctx, ret)
}

// Add 新增
func (*scoreRuleController) Add(ctx *gin.Context) {
	request := new(adminRequest.AddScoreRuleRequest)
	err := libs.BindRequest(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	ret, err := admin.NewScoreRuleService(ctx).Add(request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.Success(ctx, ret)
}

// Edit 编辑
func (*scoreRuleController) Edit(ctx *gin.Context) {
	request := new(adminRequest.EditScoreRuleRequest)
	err := libs.BindRequest(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	err = admin.NewScoreRuleService(ctx).Edit(request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.OkToast(ctx, "编辑成功")
}

// Detail 详情
func (*scoreRuleController) Detail(ctx *gin.Context) {
	ret, err := admin.NewScoreRuleService(ctx).Detail()
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.Success(ctx, ret)
}

// Deleted 删除
func (*scoreRuleController) Deleted(ctx *gin.Context) {
	err := admin.NewScoreRuleService(ctx).Deleted()
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.OkToast(ctx, "删除成功")
}

func NewScoreRuleController() *scoreRuleController {
	return &scoreRuleController{}
}
